/**
 * 添加/更新用户组件 TODO: 将该文件的文件名从userAdd 修改为 userForm
 * Created by yuzhou on 16/9/13.
 */

define([
    'vue','debug', 'text!tpl/userAdd.html',
    'vuex/actions/user',
    'mixin/userMixin'
], function(Vue, Debug, userAddHtml, userAction, userMixin) {

    var debug = Debug('App:Main:ContentUser:UserAdd')

    return Vue.extend({
        template: userAddHtml,
        data: function(){
            return {
                title: '添加用户',
                editMode: false,
                fromUrl: '/user',
                user: {
                    sex: 0
                }
            }
        },
        vuex: {
            getters: {
                result: function(store) { return store.user.result },
                currentUser: function(store){ return store.user.currentUser }
            },
            actions: {
                addUser: userAction.addUser,
                resetResult: userAction.resetResult,
                setCurrentUser: userAction.setCurrentUser,
                resetCurrentUser: userAction.resetCurrentUser,
                editUser: userAction.editUser
            }
        },
        components: {
        },
        route: {
            activate: function(transition){
                if(transition.from.name) {
                    //this.fromUrl = transition.from.path :TODO path中含有url#前的参数，需要排除
                }
                //console.dir(transition.to)
                transition.next()
            },
            data: function(transaction){
                this.resetResult()
                var id = transaction.to.params.id
                if(id) {
                    var me = this
                    this.setCurrentUser(id, function(user){
                        console.log('callback after set current user to: ', user)
                        me.user = JSON.parse(JSON.stringify(user))
                    })
                    return {
                        title: '修改用户' + id,
                        editMode: true
                    }
                } else {
                    this.resetCurrentUser()
                }
            }
        },
        validators: {
            cnname: function(val) {
                return val === '' || /^[\u4e00-\u9fa5]{2,5}$/.test(val)
            }
        },
        computed: {
            usernameValid: function(){
                return this.$userValidation.username.touched && this.$userValidation.username.valid
            },
            usernameInvalid: function(){
                return this.$userValidation.username.touched && this.$userValidation.username.invalid
            },
            usernameError: function(){
                var error = '数据不正确'
                if(this.$userValidation.username.required) error = '用户名为必填项'
                else if(this.$userValidation.username.minlength) error = '用户名的最小长度为6'
                return error
            },
            passwordValid: function(){
                return this.$userValidation.password.touched && this.$userValidation.password.valid
            },
            passwordInvalid: function(){
                return this.$userValidation.password.touched && this.$userValidation.password.invalid
            },
            passwordError: function(){
                var error = '数据不正确'
                if(this.$userValidation.password.required) error = '密码为必填项'
                else if(this.$userValidation.password.minlength) error = '密码的最小长度为6'
                return error
            },
            realNameValid: function(){
                return this.$userValidation.realName.touched && this.$userValidation.realName.valid && this.user.realName
            },
            realNameInvalid: function(){
                return this.$userValidation.realName.touched && this.$userValidation.realName.invalid && this.user.realName
            },
            realNameError: function(){
                var error = '数据不正确'
                if(this.$userValidation.realName.cnname) error = '姓名格式不正确，正确格式为2~5个中文字符'
                return error
            },
            emailValid: function(){
                return this.$userValidation.email.touched && this.$userValidation.email.valid && this.user.email
            },
            emailInvalid: function(){
                return this.$userValidation.email.touched && this.$userValidation.email.invalid && this.user.email
            },
            emailError: function(){
                var error = '数据不正确'
                if(this.$userValidation.email.email) error = '邮箱格式不正确'
                return error
            },
            mobileValid: function(){
                //console.log(this.$userValidation.mobile.valid)
                return this.$userValidation.mobile.touched && this.$userValidation.mobile.valid && this.user.mobile
            },
            mobileInvalid: function(){
                return this.$userValidation.mobile.touched && this.$userValidation.mobile.invalid && this.user.mobile
            },
            mobileError: function(){
                var error = '数据不正确'
                if(this.$userValidation.mobile.mobile) error = '手机号码格式不正确'
                return error
            }
        },
        watch: {
        },
        methods: {
            submit: function(){
                var me = this
                if(me.editMode) {
                    var changed = {}
                    for(var key in me.user){
                        if(me.user[key] != me.currentUser[key]) {
                            changed[key]  = me.user[key]
                        }
                    }

                    if(Object.keys(changed).length == 0) {
                        alert('没有检测到任何修改')
                    } else {
                         this.$validate(true, function () {
                             if(me.$userValidation.valid){
                                 me.editUser(me.user.id, changed, function(user){
                                     me.user = JSON.parse(JSON.stringify(user))
                                     me.$resetValidation()
                                 })
                             }
                         })
                    }
                } else {
                    this.$validate(true, function () {
                        if(me.$userValidation.valid){
                            me.addUser(me.user)
                        }
                    })
                }
            }
        },
        mixins: [userMixin],
        ready: function(){
            debug('Main ContentUser UserAdd Component is ready')
            this.$resetValidation()
        }
    })
})
